<?php
define('IN_TG', 'member_friend');
define('IN_JS', 'member_message');
define('SCRIPT', 'member_friend');
session_start();
require('includes/common.inc.php');
//加载头部文件
require('header.php');
//分页计算
$size = 10;
$sql = "select tg_id,tg_touser,tg_fromuser from tg_friend where (tg_touser ='{$_SESSION['username']}' or tg_fromuser ='{$_SESSION['username']}') and tg_state=1";
_page($sql, $size);
$sql .= " order by tg_id limit $_pagenum,$size";
$res = _query($sql);
$friend_arr = array();
while ($row = _fetch_array_list($res)) {
    $friend_arr[] = $row;
}
if (!empty($_POST)) {
    if ($_GET['action'] == 'delete') {
        if (isset($_POST['delete_ids'])) {
            if (isset($_POST['ids']) && !empty($_POST['ids']) > 0) {
                $ids = implode(',', $_POST['ids']);
                $sql = "delete from tg_friend where tg_id in ({$ids})";
                _query($sql);
                header("location:my_friend.php");
            } else {
                _alert_back('请进行选择，再做进一步操作');
            }
        }
    }
}
?>
    <div id="member">
        <?php
        require("includes/member.inc.php");
        ?>
        <div id="member_main">
            <h2>我的好友</h2>
            <?php if (count($friend_arr) > 0): ?>
                <form method="post" action="?action=delete">
                    <table cellspacing="1">
                        <tr>
                            <th>好友名</th>
                            <th>操作</th>
                        </tr>
                        <?php foreach ($friend_arr as $row): ?>
                            <tr>
                                <td><?php echo $row['tg_touser'] == $_COOKIE['username'] ? $row['tg_fromuser'] : $row['tg_touser']; ?></td>
                                <td><input name="ids[]" value="<?php echo $row['tg_id'] ?>" type="checkbox"
                                           class="ids"/></td>
                            </tr>
                        <?php endforeach; ?>
                        <tr>
                            <td colspan="5">
                                <label for="all">全选 <input type="checkbox" name="chkall" id="all"/></label>
                                <input type="submit" value="删除好友" name="delete_ids" id="delete_ids"/>
                            </td>
                        </tr>
                    </table>
                </form>
                <div id="page_text"><?php _paging(2) ?></div>
            <?php else: ?>
                <h3 style="text-align:center;margin-top:20px;">暂无相关数据</h3>
            <?php endif ?>
        </div>
    </div>

<?php
require('includes/footer.inc.php');
?>